package com.mnkjxy.beauty.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 判断网络连接是否正常的工具类
 * 
 * @author lqp
 * 
 */
public class NetUtils {

	/**
	 * 判断是否有网络连接
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkConnection(Context context) {
		boolean isWifi = isWifi(context);
		boolean isBobile = isMobile(context);

		if (isWifi || isBobile) {
			return true;
		}

		return false;
	}

	/**
	 * @param context
	 * @return 是否有wifi连接
	 */
	private static boolean isWifi(Context context) {
		// TODO Auto-generated method stub
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (info != null && info.isConnected()) {
			return true;
		}

		return false;
	}

	/**
	 * 是否有移动网络连接
	 * 
	 * @param context
	 * @return
	 */
	private static boolean isMobile(Context context) {
		// TODO Auto-generated method stub
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm
				.getNetworkInfo(ConnectivityManager.TYPE_BLUETOOTH);
		if (info != null && info.isConnected()) {
			return true;
		}
		return false;
	}
}
